/**
 * \file LabelHeap.h
 *
 * \brief The data block in a file representing all package labels
 * 
 * The structure holds the very data related to the label in a
 * specific package. For example if a determined package holds the
 * labels:
 *  - public STUDENTS;
 *  - package ADMIN;
 *  - public PROFESSORS;
 *
 * this structure contains the data regarding the two *public* labels
 * STUDENTS and PROFESSORS;
 *
 * The data is stored as following figure:
 * 
 * <table>
 * 	<tr>
 * 		<th>field size</th>
 * 		<th>Name</th>
 * 		<th>Type</th>
 * 		<th>Description</th>
 * 	</tr>
 * 	<tr>
 * 		<td>1</td>
 * 		<td>\f$ n_1 \f$</td>
 * 		<td>unsigned int</td>
 * 		<td>represents the length of the hash table</td>
 * 	</tr>
 * 	<tr>
 * 		<td>1</td>
 * 		<td>\f$ n_2\f$ </td>
 * 		<td>unsigned int</td>
 * 		<td>represents the second member of the remainder of the second hash</td>
 * 	</tr>
 * 	<tr>
 * 		<td>1</td>
 * 		<td>heapsize</td>
 * 		<td>unsigned int</td>
 * 		<td>represents the size of the heap. With this data the compiler can easily set the correct space to allocate in memory</td>
 * 	</tr>
 * 	<tr>
 * 		<td>\f$ n_1\f$</td>
 * 		<td>hashtable</td>
 * 		<td>array of unsigned int</td>
 * 		<td>represents an array of \f$ n_1\f$ cells containing the relative address of data in the <i>data heap</i></td>
 * 	</tr>
 * 	<tr>
 * 		<td>1</td>
 * 		<td>padding</td>
 * 		<td>no type</td>
 * 		<td>Use to point the NULL value</td>
 * 	</tr>
 * 	<tr>
 * 		<td>heapsize</td>
 * 		<td>data heap</td>
 * 		<td>no type</td>
 * 		<td>
 * 			represents the area of the file where the actual data are stored.
 * 			Each structure is composed by 2 things: the key itself and the data itself.
 * 			The key is important to evaluate wether or not a cell in the hashtable is empty or not
 * 		</td>
 * 	</tr>
 * </table>
 *
 *
 * The field <i>hastable</i> represents an open address hashtable type; moreover, the hash is computed through the double hash method:
 * \f[
 * 	h(k,i)=(h_1(k)+i{h_2}(k))mod n_1
 * \f]\f[
 * 	h_1(k)=k mod n_1
 * \f]\f[
 * 	h_2(k)=1+ (k mod n_2)
 * \f]
 *
 * The default value inside the hashtable (representing the NULL value) is 0: in this way if a cell of the hashtable is empty the hash
 * will point to the "padding" area, an important area which means <i>no value is present in the cell</i>.
 * If you want to read further about this type of hash, plase visit the page http://en.wikipedia.org/wiki/Double_hashing
 *
 * In the LabelHeap the values are array of chars representing strings.
 * Let's make clearer the hashtable itself:
 * let's assume we need to store "hello" in the hashtable at the index 5.
 * To make this happen we write at the sixth position (the zero takes a position too)
 * of the array "hashtable" an integer. The integer indicates the difference between the real
 * position of the first char in the string "hello" in the "heap" values and the position
 * where values starts. In this way in hashtable there are just numbers while the true
 * values are store in the heap "values".
 *
 * An obvious note about this type of heap handled by the hashtable is that the structure can holds up to \f$ n_1 \f$ elements: if you need
 * to store more than that, you have to increase the number \f$ n_1 \f$ .
 *
 * \date Nov 15, 2013
 * \author koldar
 * \version 1.0
 */

#ifndef LABELHEAP_H_
#define LABELHEAP_H_


#include <stdbool.h>
#include <stdio.h>
#include "Label.h"

/**Synthetizes all the information regarding a LabelHeap inside a single sturcture
 *
 */
typedef struct LabelHeapInfo {
	unsigned int n_1; //!< represents the size of the hashtable and the modulo of the first hash in the double hash operation
	unsigned int n_2; //!< represents the modulo of the second hash in the double hash operation
	unsigned int heapsize; //!< represents the size of the heap associated to this LabelHeap
	unsigned int* hashtable; //!< pointer to the hashtable
	void* heap; //!< pointer to the heap where all the data is stored. Eaxch data is a couple structured as: \li DirectHash of the label field of Label; \li Label itself;
} LabelHeapInfo;

/**Pointer to a LabelHeapInfo
 *
 */
typedef LabelHeapInfo* PlabelHeapInfo;

/**@brief loads labeldata from a file into the memory
 *
 * The function copies the data in the file store inside the memory heap of the compiler.
 * Note that the file will <b>not</b> be closed inside the funciton: so it <b>has</b> to be closed manually.
 *
 * \pre
 *  \li store already open;
 *  \li store contains LabelData correctly formatted;
 * \post
 *  \li all the data in stored is copied in block in the memory
 *
 * @param store the file used as source of the data to copy in memory
 * @return the pointer of the LabelHeap stored in memory
 */
PlabelHeapInfo loadLabelHeap(FILE* store);

/**@brief find wether or not a Label is stored in this LabelHeap
 *
 *
 * @param labeltofind Label that has to be looked for
 * @param heap the heap to search into
 * @return a pointer to the Label if it is present in the heap, NULL otherwise
 */
Plabel findLabelData(Plabel labeltofind, PlabelHeapInfo heap);

//TODO docuemtation
void printLabelData(FILE* f,PlabelHeapInfo heap);

//TODO docuemtation
void freeLabelData(PlabelHeapInfo heap);

#endif /* LABELHEAP_H_ */
